package com.test.dto;

import java.io.Serializable;

/**
 * 对于其他情况的返回，建议将code码定义在constant中
 *
 * @author ruoyi
 */
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    // 另外的常量类，这里示例使用内部类演示
    private static class HttpStatusConstant {
        static final String SUCCESS = "200";
        static final String FAIL = "500";
        static final String PARAM_ERROR = "1001";
    }

    /**
     * 成功
     */
    public static final String SUCCESS = HttpStatusConstant.SUCCESS;

    /**
     * 失败
     */
    public static final String FAIL = HttpStatusConstant.FAIL;

    private String code;

    private String msg;

    private T data;

    public static <T> R<T> paramErr() {
        return restResult(null, HttpStatusConstant.PARAM_ERROR, "参数格式错误");
    }

    public static <T> R<T> paramErr(String msg) {
        return restResult(null, HttpStatusConstant.PARAM_ERROR, msg);
    }

    public static <T> R<T> ok() {
        return restResult(null, SUCCESS, null);
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, SUCCESS, null);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> R<T> fail() {
        return restResult(null, FAIL, null);
    }

    public static <T> R<T> fail(String msg) {
        return restResult(null, FAIL, msg);
    }

    public static <T> R<T> fail(T data) {
        return restResult(data, FAIL, null);
    }

    public static <T> R<T> fail(T data, String msg) {
        return restResult(data, FAIL, msg);
    }

    public static <T> R<T> fail(T data, String code, String msg) {
        return restResult(data, code, msg);
    }

    private static <T> R<T> restResult(T data, String code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
